package com.shrct.businessprocess.process_test.impl;

import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.process_test.MyProcess;
import com.shrct.businessprocess.process_test.proxy.MyProcessProxy;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.workflow.ActivityNodeDef;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.langlab.domain.workflow.impl.BusinessProcessImpl;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@DiscriminatorValue("MYPROCESS_TYPE")
public class MyProcessImpl extends BusinessProcessImpl implements MyProcess {
  public MyProcessImpl() {
    super();
  }
  
  public String getProcessName() {
    return "我的流程";
  }
  
  public String getProcessType() {
    return "MyProcess";
  }
  
  public ActivityNodeDef getActivityDef(final String activityId) {
    edu.fudan.langlab.domain.workflow.ActivityNodeDef result = new edu.fudan.langlab.domain.workflow.ActivityNodeDef();
    result.setActivityId(activityId);
    if("__end__".equals(activityId)){
    	result.setDescription("end");
    }else
    	result.setDescription(Enum.valueOf(com.shrct.businessprocess.process_test.MyProcessStatus.class, activityId).toString());
    return result;
    
  }
  
  public void _start(final RCEmployee sale, final RCEmployee manager, final Token token) {
    this.myProcessSale = sale;
    this.myProcessManager = manager;
  }
  
  public void start(final RCEmployee sale, final RCEmployee manager) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().start(this,com.shrct.businessprocess.process_test.MyProcessStatus.apply.name());
    _start(sale,manager,token);
    getProcessManager().registTask(this, token, sale(),getApply_task_message());
    systemExecute();
    String msg = getStart_log_message(sale,manager);
    getProcessManager().createActivity(this,"Create Process",msg);
    
    
  }
  
  public String getStart_log_message(final RCEmployee sale, final RCEmployee manager) {
    String _name = sale.getName();
    String _plus = ("\u9500\u552E" + _name);
    String _plus_1 = (_plus + "\u53D1\u8D77\u6D41\u7A0B");
    return _plus_1;
  }
  
  public void _apply(final Token token) {
    Token evaluate_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.process_test.MyProcessStatus.evaluate.name());
    getProcessManager().registTask(this, evaluate_token,manager(),getEvaluate_task_message());
  }
  
  public void apply() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.apply.name());
    _apply(token);
    systemExecute();
    String msg = getApply_log_message();
    getProcessManager().createActivity(this,"Apply",msg);
    
  }
  
  public String getApply_log_message() {
    return "";
  }
  
  public String getApply_task_message() {
    return "";
  }
  
  public void evaluate() {
  }
  
  public void _evaluate_reject(final Token token) {
    Token apply_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.process_test.MyProcessStatus.apply.name());
    getProcessManager().registTask(this, apply_token,sale(),getApply_task_message());
  }
  
  public void evaluate_reject() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.evaluate.name());
    _evaluate_reject(token);
    systemExecute();
    String msg = getEvaluate_Reject_log_message();
    getProcessManager().createActivity(this,"Evaluate.拒绝",msg);
    
    
  }
  
  public String getEvaluate_Reject_log_message() {
    return "";
  }
  
  public void _evaluate_confirm(final Token token) {
    Token confirmFork_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.process_test.MyProcessStatus.confirmFork.name());
    
  }
  
  public void evaluate_confirm() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.evaluate.name());
    _evaluate_confirm(token);
    systemExecute();
    String msg = getEvaluate_Confirm_log_message();
    getProcessManager().createActivity(this,"Evaluate.同意",msg);
    
    
  }
  
  public String getEvaluate_Confirm_log_message() {
    return "";
  }
  
  public String getEvaluate_task_message() {
    return "";
  }
  
  public void _confirmFork(final Token token) {
    Token wireMoney_token = getProcessManager().fork(token,com.shrct.businessprocess.process_test.MyProcessStatus.wireMoney.name());
    getProcessManager().registTask(this, wireMoney_token,sale(),getWireMoney_task_message());
    Token achieve_token = getProcessManager().fork(token,com.shrct.businessprocess.process_test.MyProcessStatus.achieve.name());
    getProcessManager().registTask(this, achieve_token,manager(),getAchieve_task_message());
  }
  
  public void confirmFork() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.confirmFork.name());
    getProcessManager().prepareFork(token,com.shrct.businessprocess.process_test.MyProcessStatus.commit.name());
    _confirmFork(token);
    getProcessManager().tokenForked(token);
    systemExecute();
    String msg = getConfirmFork_log_message();
    getProcessManager().createActivity(this,"Fork",msg);					
    
  }
  
  public String getConfirmFork_log_message() {
    return "";
  }
  
  public void _wireMoney(final String comment, final Double amount, final Token token) {
    getProcessManager().join(token);
  }
  
  public void wireMoney(final String comment, final Double amount) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.wireMoney.name());
    _wireMoney(comment,amount,token);
    systemExecute();
    String msg = getWireMoney_log_message(comment,amount);
    getProcessManager().createActivity(this,"Wire Money",msg);
    
  }
  
  public String getWireMoney_log_message(final String comment, final Double amount) {
    return "";
  }
  
  public String getWireMoney_task_message() {
    return "";
  }
  
  public void _achieve(final Token token) {
    getProcessManager().join(token);
  }
  
  public void achieve() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.achieve.name());
    _achieve(token);
    systemExecute();
    String msg = getAchieve_log_message();
    getProcessManager().createActivity(this,"Achieve",msg);
    
  }
  
  public String getAchieve_log_message() {
    return "";
  }
  
  public String getAchieve_task_message() {
    return "";
  }
  
  public void _commit(final Token token) {
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void commit() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.MyProcessStatus.commit.name());
    _commit(token);
    systemExecute();
    String msg = getCommit_log_message();
    getProcessManager().createActivity(this,"Commit",msg);
    
  }
  
  public String getCommit_log_message() {
    return "";
  }
  
  public String getCommit_task_message() {
    return "";
  }
  
  @JoinColumn(name = "myProcessSale_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee myProcessSale;
  
  public RCEmployee getMyProcessSale() {
    return this.myProcessSale;
  }
  
  public MyProcess setMyProcessSale(final RCEmployee myProcessSale) {
    this.myProcessSale = myProcessSale;
    return this;			
    
  }
  
  @JoinColumn(name = "myProcessManager_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee myProcessManager;
  
  public RCEmployee getMyProcessManager() {
    return this.myProcessManager;
  }
  
  public MyProcess setMyProcessManager(final RCEmployee myProcessManager) {
    this.myProcessManager = myProcessManager;
    return this;			
    
  }
  
  public boolean _systemExecute() {
    boolean stateChanged = false;
    if(getProcessManager().tokenPresent(this,com.shrct.businessprocess.process_test.MyProcessStatus.confirmFork.name())){
    	confirmFork();
    	stateChanged = true;
    }	
    return stateChanged;
  }
  
  public static MyProcess create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.process_test.MyProcess myProcess = new com.shrct.businessprocess.process_test.impl.MyProcessImpl();
    objectFactory.create(myProcess);
    return myProcess;			
    
  }
  
  public Party sale() {
    return this.myProcessSale;
  }
  
  public Party manager() {
    return this.myProcessManager;
  }
  
  public MyProcessProxy toProxy() {
    com.shrct.businessprocess.process_test.proxy.MyProcessProxy proxy = new com.shrct.businessprocess.process_test.proxy.MyProcessProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
